﻿using EMS.Entities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.OracleClient;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EMS
{
    public partial class Login : DevExpress.XtraEditors.XtraForm
    {
        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;

        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd,
                         int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        string connectionString = ConfigurationManager.ConnectionStrings["OrlcConnect"].ConnectionString;

        bool fag = false;

        public delegate void passStaff(Staff staff, string name);

        public passStaff loginConnect;
        public Login()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            if (!fag)
            {
                Application.Exit();
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            using (var connection = new OracleConnection(connectionString))
            {
                connection.Open();
                OracleTransaction trans = connection.BeginTransaction();
                var cmd = new OracleCommand()
                {
                    Connection = connection,
                    CommandText = String.Format(@"SELECT s.staffid, s.username, s.password, p.name 
                                                    FROM staff s, person p 
                                                    WHERE s.personid = p.personid and s.username = '{0}' and s.password = '{1}'",
                                                txtUsername.Text, txtPassword.Text),
                    CommandType = CommandType.Text,
                    Transaction = trans
                };
                try
                {
                    OracleDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        Staff st = new Staff()
                        {
                            StaffId = reader.GetInt32(0),
                            UserName = reader.GetString(1),
                            Password = reader.GetString(2)
                        };
                        string name = reader.GetString(3);
                        loginConnect(st, name);
                        fag = true;
                    }
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    MessageBox.Show(ex.Message);
                }
            }
            if (fag) { this.Close(); }
            else
            {
                MessageBox.Show("Username or password is invalid");
            }
        }

        private void Login_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }

        }
    }
}
